package com.pdg.android.splorr.states;

import android.content.DialogInterface;

import com.pdg.android.common.ParentedBase;
import com.pdg.android.common.State;
import com.pdg.android.splorr.R;
import com.pdg.android.splorr.SplorrActivity;
import com.pdg.android.splorr.Utilities;
import com.pdg.android.splorr.data.Game;
import com.pdg.android.splorr.data.enums.GameState;


public class MainMenuState extends State<Game,SplorrActivity,GameState> {

	public class OverwriteDialogHandler extends ParentedBase<MainMenuState> implements DialogInterface.OnClickListener{
		public OverwriteDialogHandler(MainMenuState theParent){
			super(theParent);
		}
		public void onClick(DialogInterface theDialogInterface, int theWhich) {
			switch(theWhich){
			case DialogInterface.BUTTON_POSITIVE:
				getParent().startGame();
				break;
			case DialogInterface.BUTTON_NEGATIVE:
				//do nothing
				break;
			}
		}
	}
	
	public MainMenuState(Game theData, SplorrActivity theMachine) {
		super(theData, theMachine, GameState.MAIN_MENU);
	}

	@Override
	protected void onInitialize() {
	}

	@Override
	protected void onEnter() {
		getMachine().setContentView(R.layout.main_menu);
		refreshDisplay();
	}

	private void refreshDisplay() {
		Utilities.setButtonEnabled(this,R.id.button_resume, getData().isGameInProgress());
	}

	@Override
	protected void onLeave() {
	}

	@Override
	protected void onCleanUp() {
	}

	@Override
	protected void onButtonPressed(int theButtonId) {
		switch(theButtonId){
		case R.id.button_about:
			getMachine().setCurrent(GameState.ABOUT);
			break;
		case R.id.button_how_to_play:
			getMachine().setCurrent(GameState.HOW_TO_PLAY);
			break;
		case R.id.button_resume:
			resumeGame();
			break;
		case R.id.button_start:
			if(getData().isGameInProgress()){
				showOverwriteDialog();
			}else{
				startGame();
			}
			break;
		}
	}

	private void startGame() {
		getData().newGame(0);
		getMachine().setCurrent(getData().getGameStateForPlayerState());
	}

	private void resumeGame() {
		getMachine().setCurrent(getData().getGameStateForPlayerState());
	}

	private void showOverwriteDialog() {
		getData().getUtilities().confirmDialog(R.string.dialog_title_are_you_sure, R.string.dialog_message_game_data_loss, R.string.dialog_button_yes, R.string.dialog_button_no, new OverwriteDialogHandler(this));
	}


}
